package com.scpii.universal.struts.manage;

import java.io.File;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.doone.comm.paginaction.Pagination;
import com.scpii.universal.action.BaseAction;
import com.scpii.universal.constant.GenericConstants;
import com.scpii.universal.dto.AppManageDto;
import com.scpii.universal.dto.EnterMiscroBoDto;
import com.scpii.universal.hibernate.entity.UserWeiboTab;
import com.scpii.universal.service.AppManageService;
import com.scpii.universal.service.TypeService;
import com.scpii.universal.util.ImgAndFilePath;
import com.scpii.universal.util.StreamUtil;

/**
 * 
 * universalWeb
 * 
 * @author zhangqiang 2012-7-28下午03:34:09
 */
@Controller
@Scope("prototype")
public class AppManageAction extends BaseAction {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 6465504781641211513L;
	@Resource
	private AppManageService appManageService;
	@Resource
	private TypeService typeService;
	// 应用信息列表
	private List<AppManageDto> appList;

	// 上传的文件流
	private File myFile;
	// 上传文件的文件名称
	private String myFileFileName;
	private Integer intAppId;
	// 微博主键
	private Integer intWeiboId;

	// 应用信息
	private AppManageDto appManageDto;

	// 分页信息
	private Pagination page = new Pagination();

	// 查询条件
	private EnterMiscroBoDto condition;
	// 微博信息
	private EnterMiscroBoDto enterMiscroBoDto;
	// 微博list
	private List<EnterMiscroBoDto> microBoList;

	// 微博信息
	private UserWeiboTab weiboTab;

	// 微博类型
	private Map<Integer, String> weiBoType;

	// 应用类型
	private Integer intAppOsType;

	public AppManageDto getAppManageDto() {
		return appManageDto;
	}

	public void setAppManageDto(AppManageDto appManageDto) {
		this.appManageDto = appManageDto;
	}

	public List<AppManageDto> getAppList() {
		return appList;
	}

	public void setAppList(List<AppManageDto> appList) {
		this.appList = appList;
	}

	/**
	 * 获取app信息
	 * 
	 * @return
	 * @author zhangqiang 2012-7-28下午03:00:45
	 */
	public String getAppInfo() {

		// 获取应用类型
		Map<Integer, String> osType = typeService
				.getTypeListByParentId(GenericConstants.APP_OS_TYPE_ID);
		appList = appManageService.getAppInfo(getUserInfo(), intAppOsType);
		if (appList != null && appList.size() > 0) {
			for (AppManageDto appManageDto : appList) {
				appManageDto.setVarAppOsTypeName(osType.get(appManageDto
						.getIntAppOsType()));
				appManageDto.setVarIconUrl(getServerURL()
						+ appManageDto.getVarIconUrl());
			}
		}

		return SUCCESS;
	}

	/**
	 * 获取用于编辑信息
	 * 
	 * @return
	 * @author zhangqiang 2012-7-29下午11:00:46
	 */
	public String goEditAppInfoById() {
		appList = appManageService.getAppInfo(getUserInfo(), intAppOsType);
		if (appList != null && appList.size() > 0) {
			appManageDto = appList.get(0);
			// 获取应用类型
			Map<Integer, String> osType = typeService
					.getTypeListByParentId(GenericConstants.APP_OS_TYPE_ID);
			appManageDto.setVarAppOsTypeName(osType.get(appManageDto
					.getIntAppOsType()));
			appManageDto.setVarIconUrl(getServerURL()
					+ appManageDto.getVarIconUrl());
		}
		return SUCCESS;
	}

	/**
	 * 保存编辑信息
	 * 
	 * @return
	 * @author zhangqiang 2012-7-29下午11:01:58
	 */
	public String doEditApp() {
		// 得到保存图片的路径
		String filePath = "";
		if (myFile != null) {
			// 创建图片资源目录
			ImgAndFilePath.createWorkPic(getWebAppsRealPath(), getUserInfo()
					.getIntEnterpId(), getUserInfo().getAppId());
			// 文件上櫥绝对路径
			String imgAbsolutePath = getImgBasePath()
					+ ImgAndFilePath.ENTERP_ANDROID
					+ ImgAndFilePath.ENTERP_LOADING + File.separator;

			StreamUtil.uploadActiveImgs(myFile, getWebAppsRealPath()
					+ imgAbsolutePath + myFileFileName);
			filePath = getServerURL() + imgAbsolutePath
					+ myFileFileName;
		}
		// 文件保存成功 开始更改数据库

		// 文件路劲
		appManageDto.setVarIconUrl((filePath).replace("\\", "/"));

		appManageService.updateApp(appManageDto);

		return SUCCESS;
	}

	/**
	 * 
	 * @return
	 * @author zhangqiang 2012-7-30上午09:44:11
	 */

	public String doDelApp() {

		appManageService.deleteApp(intAppId);
		return SUCCESS;
	}

	/**
	 * 跳转到微博页面
	 * 
	 * @return
	 * @author zhangqiang 2012-7-30上午09:44:35
	 */
	public String getMicroBoInfo() {
		if (condition == null) {
			condition = new EnterMiscroBoDto();
			condition.setIntAppId(getUserInfo().getAppId());
		}
		// 判断第一次进入分页信息
		if (getSelectPerPage("page") == -1) {
			page.setPageRecord(GenericConstants.PAGE_NO);
		} else {
			page.setPageRecord(getSelectPerPage("page"));
		}
		// 判断是否是第一次查询
		if (getQueryFlag("page") == -1) {
			page.setTotalRecord(appManageService.getCountByCondition(condition));
		} else {
			page.setTotalRecord(getQueryFlag("page"));
		}
		setPageParameters(page, "page");

		// 获取微博类型
		weiBoType = appManageService.getWeoBoType();

		microBoList = appManageService.getMiscroBoInfo(condition,
				page.getCurrentPage(), page.getPageRecord());
		if (microBoList != null && microBoList.size() > 0) {
			for (EnterMiscroBoDto weiboDto : microBoList) {
				// 将微博的类型转换为汉子
				weiboDto.setWeiBoTypeName(weiBoType.get(weiboDto
						.getIntWeiboType()));
			}
		}
		return SUCCESS;

	}

	/**
	 * 微博编辑
	 * 
	 * @return
	 * @author zhangqiang 2012-7-30上午11:06:46
	 */
	public String getEditMiscroBo() {
		// 获取微博类型
		weiBoType = appManageService.getWeoBoType();
		enterMiscroBoDto = appManageService.goEditMiscroBo(intWeiboId);
		return SUCCESS;
	}

	/**
	 * 保存微博的编辑信息
	 * 
	 * @return
	 * @author zhangqiang 2012-7-30下午01:32:59
	 */
	public String doEditMiscroBo() {
		appManageService.updateWeiBO(enterMiscroBoDto);
		return SUCCESS;
	}

	/**
	 * 添加微博信息
	 * 
	 * @return
	 * @author zhangqiang 2012-7-30下午04:04:19
	 */
	public String doAddMiscroBo() {
		appManageService.saveWeiBo(enterMiscroBoDto, getUserInfo().getAppId());
		return SUCCESS;
	}

	/**
	 * 删除微博信息
	 * 
	 * @return
	 * @author zhangqiang 2012-7-30下午04:04:34
	 */
	public String doDelMiscroBo() {
		appManageService.deleteWeiBO(intWeiboId);
		return SUCCESS;

	}

	public File getMyFile() {
		return myFile;
	}

	public void setMyFile(File myFile) {
		this.myFile = myFile;
	}

	public String getMyFileFileName() {
		return myFileFileName;
	}

	public void setMyFileFileName(String myFileFileName) {
		this.myFileFileName = myFileFileName;
	}

	public Integer getIntAppId() {
		return intAppId;
	}

	public void setIntAppId(Integer intAppId) {
		this.intAppId = intAppId;
	}

	public Pagination getPage() {
		return page;
	}

	public void setPage(Pagination page) {
		this.page = page;
	}

	public EnterMiscroBoDto getCondition() {
		return condition;
	}

	public void setCondition(EnterMiscroBoDto condition) {
		this.condition = condition;
	}

	public EnterMiscroBoDto getEnterMiscroBoDto() {
		return enterMiscroBoDto;
	}

	public void setEnterMiscroBoDto(EnterMiscroBoDto enterMiscroBoDto) {
		this.enterMiscroBoDto = enterMiscroBoDto;
	}

	public List<EnterMiscroBoDto> getMicroBoList() {
		return microBoList;
	}

	public void setMicroBoList(List<EnterMiscroBoDto> microBoList) {
		this.microBoList = microBoList;
	}

	public UserWeiboTab getWeiboTab() {
		return weiboTab;
	}

	public void setWeiboTab(UserWeiboTab weiboTab) {
		this.weiboTab = weiboTab;
	}

	public Integer getIntWeiboId() {
		return intWeiboId;
	}

	public void setIntWeiboId(Integer intWeiboId) {
		this.intWeiboId = intWeiboId;
	}

	public Map<Integer, String> getWeiBoType() {
		return weiBoType;
	}

	public void setWeiBoType(Map<Integer, String> weiBoType) {
		this.weiBoType = weiBoType;
	}

	public Integer getIntAppOsType() {
		return intAppOsType;
	}

	public void setIntAppOsType(Integer intAppOsType) {
		this.intAppOsType = intAppOsType;
	}

}
